qx.Class.define("elpintao.frms.remitos.pageRemitosEmi",
{
	extend : qx.ui.tabview.Page,
	construct : function (application)
	{
		this.base(arguments);
		
	this.setLabel('Remitos emitidos');
	this.setLayout(new qx.ui.layout.Canvas());
	this.toggleShowCloseButton();
		
	this.addListenerOnce("appear", function(e){
		tblPaciente.focus();
	});
	
	
	var rowDataRemito = null;
	
	
	
	//Menu de contexto Pedido
	
	var menutblRemito = new controles.menu.Menu();
	var btnAutorizar = new qx.ui.menu.Button("Autorizar...");
	btnAutorizar.setEnabled(false);
	btnAutorizar.addListener("execute", function(e) {
		var win = new elpintao.frms.remitos.windowAutorizaRemito(application, rowDataRemito, true);
		win.addListener("actualizar", function(e){
			var rpc = new qx.io.remote.Rpc("services/", "elpintao.Remitos");
			try {
				var resultado = rpc.callSync("leer_remitos_emi");
			} catch (ex) {
				alert("Sync exception: " + ex);
			}
			
			tblPaciente.setFocusedCell();
			tableModel.setDataAsMapArray(resultado, true);
			tblPaciente.buscar("id_remito_emi", e.getData());
			tblPaciente.focus();
		});
		win.addListener("aceptado", function(e){
			var rpc = new qx.io.remote.Rpc("services/", "elpintao.Remitos");
			try {
				var resultado = rpc.callSync("leer_remitos_emi");
			} catch (ex) {
				alert("Sync exception: " + ex);
			}
			
			tblPaciente.setFocusedCell();
			tableModel.setDataAsMapArray(resultado, true);
			tblPaciente.buscar("id_remito_emi", e.getData());
			tblPaciente.focus();
			application.timerTransmision.fireEvent("interval");
			btnImprimir.execute();
		});
		win.setModal(true);
		application.getRoot().add(win);
		win.center();
		win.open();
	});
	var btnImprimir = new qx.ui.menu.Button("Imprimir");
	btnImprimir.setEnabled(false);
	btnImprimir.addListener("execute", function(e){
		window.open("services/class/elpintao/Impresion.php?rutina=imprimir_remito&emitir=true&id_remito_emi=" + rowDataRemito.id_remito_emi);
	});
	var btnNuevo = new qx.ui.menu.Button("Nuevo remito...");
	btnNuevo.addListener("execute", function(e){
		var win = new elpintao.frms.remitos.windowRemito(application, null, true);
		win.addListener("aceptado", function(e){
			var data = e.getData();

			var rpc = new qx.io.remote.Rpc("services/", "elpintao.Remitos");
			rpc.setTimeout(60000 * 1);
			rpc.callAsync(function(resultado, error, id) {
				tblPaciente.setFocusedCell();
				tableModel.setDataAsMapArray(resultado, true);
				tblPaciente.buscar("id_remito_emi", data);
				tblPaciente.focus();
				application.timerTransmision.fireEvent("interval");
			}, "leer_remitos_emi");
		});
		win.setModal(true);
		application.getRoot().add(win);
		win.center();
		win.open();
	});
	
	var btnModificar = new qx.ui.menu.Button("Modificar remito...");
	btnModificar.setEnabled(false);
	btnModificar.addListener("execute", function(e){
		var win = new elpintao.frms.remitos.windowRemito(application, rowDataRemito, true);
		win.addListener("aceptado", function(e){
			var data = e.getData();

			var rpc = new qx.io.remote.Rpc("services/", "elpintao.Remitos");
			rpc.setTimeout(60000 * 1);
			rpc.callAsync(function(resultado, error, id) {
				tblPaciente.setFocusedCell();
				tableModel.setDataAsMapArray(resultado, true);
				tblPaciente.buscar("id_remito_emi", data);
				tblPaciente.focus();
				application.timerTransmision.fireEvent("interval");
			}, "leer_remitos_emi");
		});
		win.setModal(true);
		application.getRoot().add(win);
		win.center();
		win.open();
	});
	
	menutblRemito.add(btnAutorizar);
	menutblRemito.add(btnImprimir);
	menutblRemito.addSeparator();
	menutblRemito.add(btnNuevo);
	menutblRemito.add(btnModificar);
	menutblRemito.memorizar();
	
		
		
		//Tabla

		
		var tableModel = new qx.ui.table.model.Simple();
		tableModel.setColumns(["Nro.remito", "Para", "Transporta", "Autoriza", "Fecha", "Estado"], ["nro_remito", "destino_descrip", "transporta", "autoriza", "fecha", "estado_descrip"]);
		//tableModel.setDataAsMapArray(resultado.contacto, true);

		var custom = {tableColumnModel : function(obj) {
			return new qx.ui.table.columnmodel.Resize(obj);
		}};
		
		var tblPaciente = new controles.table.Table(tableModel, custom);
		//var tblPaciente = this._tblPaciente = new pediatras.controles.table.Table(tableModel, custom);
		tblPaciente.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
		tblPaciente.toggleColumnVisibilityButtonVisible();
		tblPaciente.toggleShowCellFocusIndicator();
		tblPaciente.toggleStatusBarVisible();

		tblPaciente.addListener("cellDblclick", function(e){

		});
		
		
		var tableColumnModel = tblPaciente.getTableColumnModel();
		//tableColumnModel.setColumnVisible(0, false);
      // Obtain the behavior object to manipulate
		var resizeBehavior = tableColumnModel.getBehavior();
		//resizeBehavior.set(0, {width:"40%", minWidth:100});
		//resizeBehavior.set(1, {width:"20%", minWidth:100});
		//resizeBehavior.set(2, {width:"40%", minWidth:100});
		
		var selectionModel = tblPaciente.getSelectionModel();
		selectionModel.addListener("changeSelection", function(e){
			if (!selectionModel.isSelectionEmpty()) {
				rowDataRemito = tableModel.getRowData(tblPaciente.getFocusedRow());
				tblDetalle.setFocusedCell();
				tableModelDetalle.setDataAsMapArray(rowDataRemito.detalle);
				btnModificar.setEnabled(rowDataRemito.estado=='R');
				btnAutorizar.setEnabled(rowDataRemito.estado=='R');
				btnImprimir.setEnabled(rowDataRemito.estado=='A');
				menutblRemito.memorizar([btnAutorizar, btnImprimir, btnModificar]);
			}
		});
		
		tblPaciente.setContextMenu(menutblRemito);
		
		this.add(tblPaciente, {left: 0, top: 0, right: 0, bottom: "45%"});
		
		
		
		
	//Tabla

	var tableModelDetalle = new qx.ui.table.model.Simple();
	tableModelDetalle.setColumns(["Fábrica", "Producto", "Capacidad", "U", "Color", "Cantidad"], ["fabrica", "producto", "capacidad", "unidad", "color", "cantidad"]);
	
	//tableModelDetalle.setEditable(true);
	//tableModelDetalle.setColumnEditable(4, true);

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblDetalle = new controles.table.Table(tableModelDetalle, custom);
	tblDetalle.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblDetalle.toggleColumnVisibilityButtonVisible();
	tblDetalle.setShowCellFocusIndicator(false);
	tblDetalle.toggleColumnVisibilityButtonVisible();
	tblDetalle.toggleStatusBarVisible();
	
	var tableColumnModelDetalle = tblDetalle.getTableColumnModel();
	//tableColumnModelDetalle.setColumnVisible(7, false);
	

	


      // Obtain the behavior object to manipulate

		var resizeBehavior = tableColumnModelDetalle.getBehavior();
		resizeBehavior.set(0, {width:"9%", minWidth:100});
		resizeBehavior.set(1, {width:"42%", minWidth:100});
		resizeBehavior.set(2, {width:"9%", minWidth:100});
		resizeBehavior.set(3, {width:"4%", minWidth:100});
		resizeBehavior.set(4, {width:"15%", minWidth:100});
		resizeBehavior.set(5, {width:"8%", minWidth:100});

		
	
	var selectionModelDetalle = tblDetalle.getSelectionModel();
	
	this.add(tblDetalle, {left: 0, top: "56%", right: 0, bottom: 0});
	
	var rpc = new qx.io.remote.Rpc("services/", "elpintao.Remitos");
	try {
		var resultado = rpc.callSync("leer_remitos_emi");
	} catch (ex) {
		alert("Sync exception: " + ex);
	}
	
	tableModel.setDataAsMapArray(resultado, true);
	if (tableModel.getRowCount()>0) tblPaciente.setFocusedCell(0, 0, true);
		
		
	},
	members : 
	{
		
	}
});